<?php
    function GenerateLink($totalItems, $pageSize, $itemsPerPage, $curPage)
    {
        $lblFirst = "Trang đầu";
        $lblPrev = "Trang trước";
        $lblNext = "Trang sau";
        $lblLast = "Trang cuối";
        $pageLink = "";
        $pagingFirst = "";
        $pagingNum = "";
        $pagingLast = "";
        $totalPage = ($totalItems % $itemsPerPage == 0) ? floor(($totalItems / $itemsPerPage)) : floor(($totalItems / $itemsPerPage)) + 1;
        if($totalItems > $itemsPerPage) // Upto 2 pages
        {
            /*---Show Fisrt Prev Page--*/
            if ($curPage > 1)
            {
                $pagingFirst .= "<li><a href=\"#1\" page=\"1\">&laquo; " . $lblFirst . "</a></li>";
                $pagingFirst .= "<li><a href=\"#" . ($curPage - 1) . "\" page=" . ($curPage - 1) . ">&lsaquo; " . $lblPrev . "</a></li>";
            }
            
            /*--Show page numbers--*/
            
            $start = $curPage - ($pageSize / 2);
            $end = $curPage + (($pageSize / 2) - 1);
            if ($start < 1)
            {
                $start = 1; 
                $end = ($pageSize > $totalPage) ? $totalPage : $pageSize;
            }
            if ($end > $totalPage)
            {
                $end = $totalPage; 
                $start = (($totalPage - $pageSize + 1) > 1) ? ($totalPage - $pageSize + 1) : 1;
            }
            
            for ($i = $start; $i <= $end; $i++)
            {
                if ($i == $curPage)
                    $pagingNum .= "<li class=\"current\">" . $i . "</li>";
                else
                    $pagingNum .= "<li><a href=\"#" . $i . "\" page=\"" . $i . "\">" . $i . "</a></li>";
            }
            
            /*---Show Next Last Page--*/
            if ($curPage < $totalPage)
            {
                $pagingLast .= "<li><a href=\"#" . ($curPage + 1) . "\" page=" . ($curPage + 1) . ">&rsaquo; " . $lblNext . "</a></li>";
                $pagingLast .= "<li><a href=\"#" . $totalPage . "\" page=\"" . $totalPage . "\">&raquo; " . $lblLast . "</a></li>";
            }
        }
        //else has 1 page
        $pageLink = $pagingFirst . $pagingNum . $pagingLast;
        return $pageLink;
    }
?>